package com.its.us.web.mapper.sys;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.its.us.web.entity.sys.Menu;
import com.its.us.web.entity.sys.Role;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @className MenuMapper
 * @author huguangjun
 * @date 2020/12/3
 * @desc 9:05
 */
public interface MenuMapper extends BaseMapper<Menu> {


    /**
     * 获取当前用户的菜单
     * @param username
     * @return
     */
    @Select("select f.* from sys_menu f where f.id in(select e.menu_id from sys_role_menu e where e.role_id in(select c.id from sys_role c where c.id in(select b.role_id from sys_user_role b where b.user_id in(select a.id from sys_user a where a.username = #{username})))) order by f.sort asc")
    List<Menu> getCurrentMenus(String username) throws Exception;

    /**
     * 获取所有的菜单
     * @return
     */
    @Select("select id,menu_id,parent_id,menu_level,menu_name,permission,menu_url,menu_des,is_show,sort,is_button,icons from sys_menu")
    List<Menu> getAllMenus() throws Exception;


    /**
     * 根据角色id查询该角色下的菜单权限
     * @param roleId
     * @return
     * @throws Exception
     */
    @Select("select menu_id from sys_role_menu where role_id = #{roleId}")
    List<Menu> getMenuByRoleId(String roleId) throws Exception;

    /**
     * del sys_role_menu 关联数据
     *
     * @param deleteIds
     */
    @Delete("<script>" +
            "delete from sys_role_menu where menu_id in" +
            "<foreach collection='deleteIds' item='menuId' open='(' separator=',' close=')'>"
            + "#{menuId}" +
            "</foreach>" +
            "</script>")
    void deleteByMenuId(@Param("deleteIds") List<String> deleteIds) throws Exception;
}
